package com.activiti.grouptask;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntityImpl;
import org.activiti.engine.impl.persistence.entity.UserEntityImpl;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组任务测试(候选组)
 * @author zhaoqx
 *
 */
public class GroupTaskTest2 {
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

	/**
	 * 部署流程定义
	 */
	@Test
	public void test1() {
		DeploymentBuilder deploymentBuilder = processEngine
				.getRepositoryService().createDeployment();
		deploymentBuilder
				.addClasspathResource("com/itheima/activiti/grouptask/bxlc2.bpmn");
		deploymentBuilder
				.addClasspathResource("com/itheima/activiti/grouptask/bxlc2.png");
		deploymentBuilder.deploy();
	}
	
	/**
	 * 创建组
	 */
	@Test
	public void test2() {
		Group group = new GroupEntityImpl();
		group.setId("财务组");
		group.setName("财务组");
		processEngine.getIdentityService().saveGroup(group );
	}
	
	/**
	 * 创建用户
	 */
	@Test
	public void test3() {
		User user = new UserEntityImpl();
		user.setId("2");
		processEngine.getIdentityService().saveUser(user);
	}
	
	/**
	 * 建立用户和组的关系
	 */
	@Test
	public void test4() {
		processEngine.getIdentityService().createMembership("2", "财务组");
	}
	
	/**
	 * 启动流程实例时设置流程变量
	 */
	@Test
	public void test5() {
		String processDefinitionKey = "bxlc";// 流程定义key
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("loginUser", "小明");
		ProcessInstance pi = processEngine.getRuntimeService()
				.startProcessInstanceByKey(processDefinitionKey, variables);
		System.out.println(pi.getId());
	}

	
	/**
	 * 办理任务时设置流程变量
	 */
	@Test
	public void test6() {
		String taskId = "1805";
		processEngine.getTaskService().complete(taskId );
	}
	
	/**
	 * 查询组任务
	 */
	@Test
	public void test7() {
		TaskQuery query = processEngine.getTaskService().createTaskQuery();
		String candidateUser = "2";
		query.taskCandidateUser(candidateUser);//使用候选人过滤
		//query.taskCandidateGroup("财务组");
		List<Task> list = query.list();
		for (Task task : list) {
			System.out.println(task.getId());
		}
	}
	
	/**
	 * 拾取组任务,将组任务变为个人任务
	 */
	@Test
	public void test8() {
		String taskId = "1902";
		processEngine.getTaskService().claim(taskId , "1");
	}
}
